<template>
  <div>
    <el-form 
			class="elForm" 
			:inline="true" 
			:model="dataForm" 
			:rules="dataRule" 
			ref="dataForm"
			>
      <el-form-item>
        <el-button 
          size="medium" 
          class="add-btn" 
          v-if="dataListSelections.length==0 && isAuth(['ROOT', 'HR'])" 
          @click="addHandle()">
          新建考核
        </el-button>
        <el-button 
          size="medium" 
          type="danger" 
          v-if="dataListSelections.length!=0 && isAuth(['ROOT', 'HR'])" 
          @click="deleteHandle()">
          批量删除
        </el-button>
      </el-form-item>
      <el-form-item prop="examName">
        <el-input v-model="dataForm.title" placeholder="考核名称" size="medium" class="input" clearable="clearable" />
        <el-button class="search" size="small" @click="searchHandle()" icon="el-icon-search"></el-button>
      </el-form-item>
    </el-form>
    <el-table 
			:data="dataList"
      ref="dataList"
			border 
			v-loading="dataListLoading"
			@selection-change="selectionChangeHandle" 
			style="width: 100%;" size="medium" 
			:header-cell-style="{borderColor: 'rgb(235,235,235)',background: 'rgb(247,247,247)'}" 
			:cell-style="{padding: '8px 0',borderColor: 'rgb(235,235,235)'}" 
			class="elTable" 
			:row-key="getRowKeys">
      <el-table-column type="selection" :selectable="selectable" header-align="center" align="center" width="50" :reserve-selection="true" />
      <el-table-column type="index" header-align="center" align="center" width="100" label="序号">
        <template #default="scope">
          <span>{{ (pageIndex - 1) * pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="title" header-align="center" align="center" label="考核名称" min-width="470" />
      <el-table-column prop="type" header-align="center" align="center" label="考核方式" min-width="140" />
      <el-table-column prop="status" header-align="center" align="center" label="状态" min-width="140" />
      <el-table-column prop="start_time" header-align="center" align="center" label="开始时间" min-width="175" />
      <el-table-column prop="end_time" header-align="center" align="center" label="结束时间" min-width="175" />
      <el-table-column header-align="center" align="center" width="80" label="操作">
        <template #default="scope">
          <el-dropdown>
            <span class="el-dropdown-link">
              <i class="el-icon-more cursor" style="font-size: 28px"></i>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item 
                  @click="$router.push({name: 'ExamCheck', params: {id: scope.row.id}})">
                  查看评分
                </el-dropdown-item>
                <el-dropdown-item 
                  @click="addHandle(scope.row.id)" 
                  v-if="isAuth(['ROOT', 'HR'])">
                  复制
                </el-dropdown-item>
                <!-- v-if="isAuth(['ROOT'])" -->
                <el-dropdown-item 
                  v-if="isAuth(['ROOT', 'HR'])"
                  @click="showUpdateHandle(scope.row)">
                  修改时间
                </el-dropdown-item>
                <el-dropdown-item 
                  v-if="isAuth(['ROOT', 'HR'])"
                  @click="deleteHandle(scope.row.id)" 
                  :disabled="dataListSelections.length!=0">
                  删除
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>
    <!-- 修改时间的弹窗 -->
    <el-dialog
      v-model="updateTimeVisible"
      title="修改时间"
      width="40%"
      :before-close="handleClose"
      >
      <div style="text-align: center;margin: 10px 0">
        <el-date-picker
          v-model="updateTimeStart"
          type="datetime"
          placeholder="开始日期">
        </el-date-picker>
        &nbsp;至&nbsp; 
        <el-date-picker
          v-model="updateTimeEnd"
          type="datetime"
          placeholder="结束日期">
        </el-date-picker>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button class="cancelBtn" @click="cancelTimeHandle" size="small">取消</el-button>
          <el-button class="add-btn" @click="updateTimeHandle" size="small">确定</el-button>
        </span>
      </template>
    </el-dialog>
    <el-pagination 
			@size-change="sizeChangeHandle" 
			@current-change="currentChangeHandle" 
			:current-page="pageIndex" 
			:page-sizes="[10, 20, 50]" 
			:page-size="pageSize" 
			:total="totalCount" 
			layout="total, sizes, prev, pager, next, jumper"
		></el-pagination>
  </div>
</template>

<script>
export default {
  data: function() {
    return {
      dataForm: {
        title: null
      },
      dataList: [],
      updateTimeVisible: false,  // 修改时间
      updateTimeStart: '',
      updateTimeEnd: '',
      updateTimeExamId: '',
      pageIndex: 1,
      pageSize: 10,
      totalCount: 0,
      dataListLoading: false,
      dataListSelections: [],
      dataRule: {
        examName: [{ required: false, pattern: '^[a-zA-Z0-9\u4e00-\u9fa5]{1,10}$', message: '角色格式错误' }]
      }
    }
  },
  methods: {
    // 修改时间
    showUpdateHandle(row) {
      this.updateTimeVisible = true
      console.log(row);
      this.updateTimeStart = row.start_time
      this.updateTimeEnd = row.end_time
      this.updateTimeExamId = row.id
    },
    updateTimeHandle() {
      let data = {
        examId: this.updateTimeExamId,
        startTime: this.updateTimeStart,
        endTime: this.updateTimeEnd
      }
      console.log(data);
      this.$http('mark/updateTime', 'post', data, true, resp => {
        console.log(resp);
        if (resp.code == 200) {
          this.loadDataList()
        }
      })
      this.cancelTimeHandle()
    },
    cancelTimeHandle() {
      this.updateTimeVisible = false
    },
    // 删除角色
    deleteHandle(id) {
      let ids = id ? [id] : this.dataListSelections.map(item => item.id)
      if (ids.length == 0) {
        this.$message({
          message: '没有选中记录',
          type: 'warning',
          duartion: 1200
        })
      } else {
        this.$confirm('确定要删除选中记录？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http('checkList/deleteMark', 'post', { ids }, true, resp => {
            if (resp.code == 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duartion: 1200
              })
              // 清空table的选中
              this.$refs.dataList.clearSelection()
              this.loadDataList()
            } else {
              this.$message({
                message: '未能删除记录',
                type: 'warning',
                duartion: 1200
              })
            }
          })
        })
      }
    },
    // 新增 复制 用户
    addHandle(id) {
      if (id == undefined) id = null
      this.$router.push({ name: 'ExamAdd', params: { id: id } })
    },
    // 查询功能
    searchHandle() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          this.$refs['dataForm'].clearValidate()
          // 后端接收到''会报错，接收null是代表没有这个条件
          if (this.dataForm.title == '') {
            this.dataForm.title = null
          }
          if (this.pageIndex != 1) {
            this.pageIndex = 1
          }
          this.loadDataList()
        } else {
          return false
        }
      })
    },
    // 加载分页数据
    loadDataList() {
      this.dataListLoading = true
      let data = {
        title: this.dataForm.title,
        page: this.pageIndex,
        length: this.pageSize
      }
      // console.log(data)

      this.$http('checkList/selectCheckListBypages', 'post', data, false, resp => {
        let page = resp.page
        console.log(page);
        this.dataList = page.list
        this.dataList.forEach(item => {
          item.type = '无序评分'
          if (item.status == 1) {
            item.status = '未开始'
          } else if (item.status == 2) {
            item.status = '进行中'
          } else if (item.status == 3) {
            item.status = '已结束'
          }
          // 处理时间中间的T 如：2021-11-14T19:11:51
          item.start_time = item.start_time.replace('T', ' ')
          item.end_time = item.end_time.replace('T', ' ')
        })
        this.totalCount = page.totalCount
        this.dataListLoading = false
      })
    },
    // 分页一页数量改变时触发
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.loadDataList()
    },
    // 分页换页时触发
    currentChangeHandle(val) {
      this.pageIndex = val
      this.loadDataList()
    },
    selectionChangeHandle: function(val) {
      this.dataListSelections = val
    },
    selectable: function(row, index) {
      // console.log(row)
      if (row.systemic || row.users > 0) {
        return false
      }
      return true
    },
    // 保存选中的数据id,row-key就是要指定一个key标识这一行的数据
    getRowKeys(row) {
      return row.id
    }
  },
  created() {
    this.loadDataList()
  }
}
</script>

<style lang="scss" scoped>
@import './exam.scss';
</style>
